/*
创建房间界面的处理逻辑
*/
var tipsConf = require("tips").tipsConf;

cc.Class({
    extends: cc.Component,

    properties: {
        parent:{
            default:null,
            type:cc.Node
        },
        isInit:false,
    },

    onLoad: function () {

    },

    onInit:function(){
        this.gameType = "PaiGow";
        this.gameTime = 10;
        this.bankerMode = 6;
        this.waitMode = 2;
        this.consumeMode = 2;
        this.awardType = 3;
        this.cardMode = 0;
        this.basicType = 0;
        this.maxBet = 100;
        this.gameTypeSelectNode = this.node.getChildByName("gameTypeSelect");
        console.log(this.gameTypeSelectNode);
        this.paigowTypeNode = this.node.getChildByName("paiGowLayer").getChildByName("type1");
        this.sanKungTypeNode = this.node.getChildByName("sanKungLayer").getChildByName("type2");

        this.awardTypeSelectNode = this.node.getChildByName("paiGowLayer").getChildByName("awardTypeSelect");
        // this.gameTypeNode = this.node.getChildByName("gameTypeNode");
        // this.gameTypeNodeList = [0,this.gameTypeNode.getChildByName("type1"),this.gameTypeNode.getChildByName("type2"),this.gameTypeNode.getChildByName("type3")];
        this.gameTypeLayerList = [0,this.node.getChildByName("paiGowLayer"),this.node.getChildByName("jinHuaLayer"),this.node.getChildByName("sanKungLayer")];
        this.paiGowToggleFalseList = [];
        this.paiGowToggleTrueList = [];
        this.sanKungToggleFalseList = [];
        this.sanKungToggleTrueList = [];

        this.paiGowToggleTrueList = [this.gameTypeLayerList[1].getChildByName("bankerModeSelect").getChildByName("toggle1").getComponent("cc.Toggle"),
                                     this.gameTypeLayerList[1].getChildByName("gameTimeSelect").getChildByName("toggle1").getComponent("cc.Toggle"),
                                     this.gameTypeLayerList[1].getChildByName("consumeSelect").getChildByName("toggle1").getComponent("cc.Toggle"),
                                     this.gameTypeLayerList[1].getChildByName("waitModeSelect").getChildByName("toggle1").getComponent("cc.Toggle"),
                                     this.gameTypeLayerList[1].getChildByName("awardTypeSelect").getChildByName("toggle1").getComponent("cc.Toggle")
                                    ];
        this.sanKungToggleTrueList = [this.gameTypeLayerList[3].getChildByName("bankerModeSelect").getChildByName("toggle1").getComponent("cc.Toggle"),
                                      this.gameTypeLayerList[3].getChildByName("gameTimeSelect").getChildByName("toggle1").getComponent("cc.Toggle"),
                                      this.gameTypeLayerList[3].getChildByName("consumeSelect").getChildByName("toggle1").getComponent("cc.Toggle"),
                                      this.gameTypeLayerList[3].getChildByName("betTypeSelect").getChildByName("toggle1").getComponent("cc.Toggle"),
                                      this.gameTypeLayerList[3].getChildByName("waitModeSelect").getChildByName("toggle1").getComponent("cc.Toggle")
                                     ];
        this.isInit = true;
    },

    btnCreateClick:function(event, customEventData){
        console.log("GameType!!!!!!!!!!" + customEventData);
        var self = this;
        var index = parseInt(customEventData);
        var createType = "";
        if(index == 1)
            createType = "newRoom";
        if(index == 2)
            createType = "agency";
        if(this.gameType == "PaiGow")
        {
            pomelo.request("connector.entryHandler.sendData", {"code" : createType,"params" : {gameType: this.gameType,
                consumeMode:this.consumeMode,gameNumber:this.gameTime,bankerMode:this.bankerMode,waitMode:this.waitMode,playerNumber:9,awardType:this.awardType}}, function(data) {
                    console.log("clientCreateRoom flag is : " + data.flag)
                    console.log(data);
                    if(data.code && data.code == 119)
                    {
                        pomelo.disconnect();
                        return;
                    }
                    if(!data.flag){
                        if(data.msg && data.msg.code)
                            window.clientGlobal.tipsBox.showLayer(1,tipsConf[data.msg.code]);
                        else
                            window.clientGlobal.tipsBox.showLayer(1,"创建房间失败,请重新创建!");
                    }else{
                        if(createType == "agency")
                            self.parent.hideCreateLayer();
                    }
                }
            );
        }else{
            var CreatorRoomInfo = {};
            if(this.bankerMode == 7)
            {
                this.basicType = 3;
                CreatorRoomInfo = {gameType: this.gameType,
                    consumeMode:this.consumeMode,gameNumber:this.gameTime,bankerMode:this.bankerMode,waitMode:this.waitMode,playerNumber:9,cardMode:0,basicType:this.basicType,maxBet:this.maxBet};
            }
            else
            {
                CreatorRoomInfo = {gameType: this.gameType,
                    consumeMode:this.consumeMode,gameNumber:this.gameTime,bankerMode:this.bankerMode,waitMode:this.waitMode,playerNumber:9,cardMode:0,basicType:this.basicType};
            }
            pomelo.request("connector.entryHandler.sendData", {"code" : createType,"params" : CreatorRoomInfo}, function(data) {
                    console.log("clientCreateRoom flag is : " + data.flag)
                    console.log(data);
                    if(data.code && data.code == 119)
                    {
                        pomelo.disconnect();
                        return;
                    }
                    if(!data.flag){
                        if(data.msg && data.msg.code)
                            window.clientGlobal.tipsBox.showLayer(1,tipsConf[data.msg.code]);
                        else
                            window.clientGlobal.tipsBox.showLayer(1,"创建房间失败,请重新创建!");
                    }else{
                        if(createType == "agency")
                            self.parent.hideCreateLayer();
                    }
                }
            );
        }
        
    },

    btnSelectGameType:function(event, customEventData){
        console.log("GameType!!!!!!!!!!" + customEventData);
        var index = parseInt(customEventData);
        if(index == 0)
            this.showLayer(1);
        else if(index == 1)
            this.showLayer(3);
        else if(index == 2)
            this.gameType = "zhajinhua"
    },

    btnSelectBankerMode:function(event, customEventData){
        //2:房主坐庄
        console.log("banker!!!!!!!!!!" + customEventData);
        this.bankerMode = parseInt(customEventData);
        if(this.gameType == "sanKung")
        {
            this.maxBet = 100;
            this.gameTypeLayerList[3].getChildByName("betLimitSelect").active = false
            this.gameTypeLayerList[3].getChildByName("betLimitSelect").getChildByName("toggle1").getComponent("cc.Toggle").isChecked = true;
            this.gameTypeLayerList[3].getChildByName("betTypeSelect").getChildByName("toggle1").getChildByName("type").getComponent(cc.Label).string = "1/2/3/4/5";
            this.gameTypeLayerList[3].getChildByName("waitModeSelect").y = 5;
        }
        if(this.bankerMode == 7)
        {
            this.awardTypeSelectNode.active = false;
            if(this.gameType == "sanKung")
            {
                this.gameTypeLayerList[3].getChildByName("betLimitSelect").active = true
                this.gameTypeLayerList[3].getChildByName("betTypeSelect").getChildByName("toggle1").getChildByName("type").getComponent(cc.Label).string = "1/5/10/15/20";
                this.gameTypeLayerList[3].getChildByName("waitModeSelect").y = -65;
            }
        }  
        else
            this.awardTypeSelectNode.active = true;
    },

    btnSelectGameTime:function(event, customEventData){
        console.log("gameTime" + customEventData);
        this.gameTime = parseInt(customEventData);
    },
    btnSelectbetLimit:function(event, customEventData){
        console.log("gameTime" + parseInt(customEventData));
        if(parseInt(customEventData) == 1)
        {
            this.maxBet = 100;
        }
        if(parseInt(customEventData) == 2)
        {
            this.maxBet = 200;
        }
        if(parseInt(customEventData) == 3)
        {
            this.maxBet = 300;
        }
        console.log("this.maxBet = " + this.maxBet)
    },

    btnSelectWaitMode:function(event, customEventData){
        console.log("WaitMode" + customEventData);
        this.waitMode = parseInt(customEventData);
    },

    btnSelectConsumeMode:function(event, customEventData){
        console.log("consumeMode" + customEventData);
        this.consumeMode = parseInt(customEventData);
    },

    btnSelectAwardType:function(event, customEventData){
        console.log("awardType" + customEventData);
        this.awardType = parseInt(customEventData);
    },

    btnSelectCardType:function(event, customEventData){
        console.log("CardType" + customEventData);
        this.cardMode = parseInt(customEventData);
    },

    btnSelectBetType:function(event, customEventData){
        console.log("betType" + customEventData);
        this.basicType = parseInt(customEventData);
    },

    hideAllToggle:function(){
        for(var i=1;i<=4;i++)
            this.gameTypeLayerList[1].getChildByName("bankerModeSelect").getChildByName("toggle"+i).getComponent("cc.Toggle").isChecked = false;
        for(var i=1;i<=2;i++)
            this.gameTypeLayerList[1].getChildByName("gameTimeSelect").getChildByName("toggle"+i).getComponent("cc.Toggle").isChecked = false;
        for(var i=1;i<=3;i++)
            this.gameTypeLayerList[1].getChildByName("consumeSelect").getChildByName("toggle"+i).getComponent("cc.Toggle").isChecked = false;
        for(var i=1;i<=3;i++)
            this.gameTypeLayerList[1].getChildByName("waitModeSelect").getChildByName("toggle"+i).getComponent("cc.Toggle").isChecked = false;
        for(var i=1;i<=3;i++)
            this.gameTypeLayerList[1].getChildByName("awardTypeSelect").getChildByName("toggle"+i).getComponent("cc.Toggle").isChecked = false;
        
        for(var i=1;i<=5;i++)
            this.gameTypeLayerList[3].getChildByName("bankerModeSelect").getChildByName("toggle"+i).getComponent("cc.Toggle").isChecked = false;
        for(var i=1;i<=2;i++)
            this.gameTypeLayerList[3].getChildByName("gameTimeSelect").getChildByName("toggle"+i).getComponent("cc.Toggle").isChecked = false;
        for(var i=1;i<=3;i++)
            this.gameTypeLayerList[3].getChildByName("consumeSelect").getChildByName("toggle"+i).getComponent("cc.Toggle").isChecked = false;
        for(var i=1;i<=3;i++)
            this.gameTypeLayerList[3].getChildByName("betTypeSelect").getChildByName("toggle"+i).getComponent("cc.Toggle").isChecked = false;
        for(var i=1;i<=3;i++)
            this.gameTypeLayerList[3].getChildByName("waitModeSelect").getChildByName("toggle"+i).getComponent("cc.Toggle").isChecked = false;
        for(var i=1;i<=3;i++)
            this.gameTypeLayerList[3].getChildByName("betLimitSelect").getChildByName("toggle"+i).getComponent("cc.Toggle").isChecked = false;
    },

    resetSelectData:function(index){
        this.gameTime = 10;
        this.waitMode = 2;
        this.consumeMode = 2;
        this.hideAllToggle();
        if(index == 1)
        {
            this.awardType = 3;
            this.gameType = "PaiGow";
            this.bankerMode = 6;
            this.awardTypeSelectNode.active = true;
            for(var i in this.paiGowToggleTrueList)
                this.paiGowToggleTrueList[i].isChecked = true;
        }
        if(index == 3)
        {
            this.gameType = "sanKung";
            this.gameTypeLayerList[3].getChildByName("waitModeSelect").y = 5;
            this.gameTypeLayerList[3].getChildByName("betLimitSelect").active = false
            this.basicType = 0;
            this.bankerMode = 2;
            for(var i in this.sanKungToggleTrueList)
                this.sanKungToggleTrueList[i].isChecked = true;
        }
    },

    showLayer:function(index,isHall){
        if(this.isInit == false)
            this.onInit();
        for(var i=1;i<=3;i++){
            // this.gameTypeNodeList[i].active = false;
            this.gameTypeLayerList[i].active = false;
        }
        if(isHall)
        {
            if(index == 4)
            {
                this.gameTypeSelectNode.active = true;
                this.gameTypeSelectNode.getChildByName("toggle1").getComponent("cc.Toggle").isChecked = true;
                this.gameTypeSelectNode.getChildByName("toggle2").getComponent("cc.Toggle").isChecked = false;
                this.paigowTypeNode.active = false;
                this.sanKungTypeNode.active = false;
            }else{
                this.gameTypeSelectNode.active = false;
                this.paigowTypeNode.active = true;
                this.sanKungTypeNode.active = true;
            }
        }
        // this.gameTypeNodeList[index].active = true;
        if(index == 4)
            index = 1;
        this.gameTypeLayerList[index].active = true;
        this.resetSelectData(index);
        this.node.active = true;
    },

    hideLayer:function(){
        this.node.active = false;
    },
});
